package com.example.aixiangshou.runninggame;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zhouwei.library.CustomPopWindow;
import com.x_memteor.base_master.application.MyApplication;
import com.x_memteor.base_master.base.BaseActivity;
import com.x_memteor.base_master.bean.GameBean;
import com.x_memteor.base_master.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectModeActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.checkbox_local)
    CheckBox checkboxLocal;
    @BindView(R.id.lay_local)
    LinearLayout layLocal;
    @BindView(R.id.checkbox_online)
    CheckBox checkboxOnline;
    @BindView(R.id.lay_online)
    LinearLayout layOnline;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_local)
    TextView tvLocal;
    @BindView(R.id.tv_online)
    TextView tvOnline;

    boolean isLocalChoose = false;
    boolean isOnlineChoose = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initRootView() {
        setContentView(R.layout.activity_select_mode);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        checkboxLocal.setOnCheckedChangeListener(this);
        checkboxOnline.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.tv_back, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_next:
                MyApplication.getInstance().getGameBean().setGameModel(GameBean.GAME_MODEL_LOCAL);
                startActivity(new Intent(SelectModeActivity.this,SelectGameActivity.class));
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.checkbox_local:

                if (isChecked) {
                    isLocalChoose = true;
                    if (isOnlineChoose){
                        checkboxOnline.setChecked(false);
                    }
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_sky));
                    tvLocal.setBackgroundColor(getResources().getColor(R.color.main_blue));
                } else {
                    isLocalChoose = false;
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_fense));
                    tvLocal.setBackgroundColor(getResources().getColor(R.color.main_gray));
                }
                break;
            case R.id.checkbox_online:
                showClosePow();
                break;
        }

    }

    CustomPopWindow popCloseWindow;

    private void showClosePow() {
        View contentView = LayoutInflater.from(SelectModeActivity.this).inflate(R.layout.pop_show, null);
        handleClose(contentView);

        popCloseWindow = new CustomPopWindow.PopupWindowBuilder(SelectModeActivity.this)
                .setView(contentView)//显示的布局
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()//创建PopupWindow
                .showAtLocation(tvOnline.getRootView(), Gravity.CENTER, 0, 0);
    }


    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleClose(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_ok:
                        if (popCloseWindow != null) {
                            popCloseWindow.dissmiss();
                        }
                        break;
                }
            }
        };
        contentView.findViewById(R.id.tv_ok).setOnClickListener(listener);

    }
}
